<?php
/**
 * 发布/订阅模式-订阅者
 * Created by PhpStorm.
 * User: august
 * Date: 2021/4/10
 * Time: 13:16
 */

namespace Aug\Tools\Design;


use SplObserver;
use SplSubject;

class Subscriber implements SplObserver
{
    protected $key;
    protected $name;
    protected $priority;
    protected $callback;

    public function __construct(string $name, callable $callback, $priority = 0)
    {
        $this->key = md5(date('YmdHis') . mt_rand(0, 9999));
        $this->name = $name;
        $this->callback = $callback;
        $this->priority = $priority;
    }

    public function update(SplSubject $publisher)
    {
        call_user_func($this->callback, $publisher);
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getKey()
    {
        return $this->key;
    }

    public function setKey($key)
    {
        $this->key = $key;
    }

    public function getPriority()
    {
        return $this->priority;
    }

    public function setPriority($priority)
    {
        $this->priority = $priority;
    }
}